﻿using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace guidHelper;

/// <summary>
/// AES-CBC加密工具类（128/192/256位密钥）
/// </summary>
public static class ToolHelper
{
    /// <summary>
    /// 根据GUID生成16位的唯一ID
    /// </summary>
    /// <returns></returns>
    public static string GUIDTO16()
    {
        long lg = 1;
        foreach (byte b in Guid.NewGuid().ToByteArray())
        {
            lg *= ((int)b + 1);
        }
        string tmp = string.Format("{0:x}", lg - DateTime.Now.Ticks);
        return tmp;
    }

    /// <summary>
    /// 唯一id生成
    /// </summary>
    /// <returns></returns>
    public static string CommonId()
    {
        var temp = Guid.NewGuid().ToString();
        return temp;
    }
    /// <summary>
    /// 将DateTime转换为年月日整数，例如2025-09-04转换为20250904
    /// </summary>
    /// <param name="dateTime">要转换的DateTime对象</param>
    /// <returns>转换后的整数</returns>
    public static int ConvertDateTimeToInt(DateTime dateTime)
    {
        // 使用ToString方法将日期格式化为"yyyyMMdd"形式的字符串
        string dateString = dateTime.ToString("yyyyMMdd");

        // 将字符串转换为整数
        if (int.TryParse(dateString, out int result))
        {
            return result;
        }

        // 转换失败时抛出异常
        throw new ArgumentException("无法将指定的日期转换为整数", nameof(dateTime));
    }
    /// <summary>
    /// 数据流转String
    /// </summary>
    /// <param name="stream"></param>
    /// <returns></returns>
    public static Task<string> StreamToStringAsync(Stream stream)
    {
        if (stream.CanSeek)
        {
            stream.Seek(0, SeekOrigin.Begin);
        }

        using (StreamReader reader = new StreamReader(stream))
        {
            var res = reader.ReadToEndAsync();
            return res;
        }
    }
    // 转换秒级时间戳为指定格式的字符串
    public static DateTime ConvertSecondsToDateTimeString(long timestamp)
    {
        // 起始时间：1970-01-01 00:00:00 UTC
        DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        // 转换为本地时间并格式化
        DateTime dateTime = startTime.AddSeconds(timestamp).ToLocalTime();
        return dateTime;
    }

    // 转换毫秒级时间戳为指定格式的字符串
    public static DateTime ConvertMillisecondsToDateTimeString(long timestamp)
    {
        DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        DateTime dateTime = startTime.AddMilliseconds(timestamp).ToLocalTime();
        return dateTime;
    }

    public static string ConvertDateTimeStringToMilliseconds(DateTime dateTime)
    {
        // 1. 将传入的本地时间转为 UTC 时间（消除时区差）
        DateTime utcDateTime = dateTime.ToUniversalTime();
        // 2. 以 UTC 基准计算时间戳
        long timestamp = (long)(utcDateTime - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
        return timestamp.ToString();
    }

    // 1. 本地时间转为秒级时间戳
    public static long LocalTimeToTimestampSeconds(DateTime localTime)
    {
        // 步骤1：本地时间转为UTC时间（关键，避免时区偏差）
        DateTime utcTime = localTime.ToUniversalTime();
        // 步骤2：计算与纪元时间的时间差
        TimeSpan timeSpan = utcTime - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        // 步骤3：转换为秒并返回（取整，忽略小数部分）
        return (long)timeSpan.TotalSeconds;
    }

    // 2. UTC时间转为秒级时间戳（若已确定是UTC时间，可跳过转UTC步骤）
    public static long UtcTimeToTimestampSeconds(DateTime utcTime)
    {
        TimeSpan timeSpan = utcTime - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        return (long)timeSpan.TotalSeconds;
    }

    // 3. 转为毫秒级时间戳（只需将TotalSeconds改为TotalMilliseconds）
    public static long LocalTimeToTimestampMilliseconds(DateTime localTime)
    {
        DateTime utcTime = localTime.ToUniversalTime();
        TimeSpan timeSpan = utcTime - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        return (long)timeSpan.TotalMilliseconds;
    }

}